﻿using System;
using System.ComponentModel;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using NNS.UI;

namespace QLTB.DieuChuyen.UI
{
    public partial class frmDieuChuyen : XtraForm
    {
        private MyExports myExports;

        public frmDieuChuyen()
        {
            InitializeComponent();
            uc.SaveChanged += uc_SaveChanged;
            uc.SaveChanging += uc_SaveChanging;
            uc.SaveComplete += uc_SaveComplete;
            Nap();
        }

        private frmMain MyRibbon
        {
            get { return ParentForm as frmMain; }
        }

        public MyExports MyExport
        {
            get
            {
                if (myExports == null)
                {
                    myExports = new MyExports();
                    myExports.ExportView = null;
                    myExports.ExportPercent += myExports_ExportPercent;
                    myExports.ExportComplete += myExports_ExportEnded;
                }
                return myExports;
            }
        }

        private void uc_SaveComplete(object sender, SaveCompleteEventArgs e)
        {
            MyRibbon.ShowProcess = BarItemVisibility.Never;
        }

        private void uc_SaveChanging(object sender, SaveChangingEventArgs e)
        {
            MyRibbon.SetValue(e.Percent);
        }

        private void uc_SaveChanged(object sender, SaveChangedEventArgs e)
        {
            if (e.NotSave)
            {
                Text = "Thanh Lý Thiết Bị (*)";
            }
            else
            {
                Text = "Thanh Lý Thiết Bị";
            }
        }

        private void Nap()
        {
            uc.Nap();
        }

        private void myExports_ExportEnded(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            MyRibbon.ShowProcess = BarItemVisibility.Never;
        }

        private void myExports_ExportPercent(object sender, ExportPercentEventArgs e)
        {
            MyRibbon.SetValue(e.Position);
        }

        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (MyRibbon != null)
            {
                // MyRibbon.MyRibbon.SetPopupContextMenu(gcList, MyRibbon.pm);
            }
        }

        private void iPdf_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Visible) return;

            MyExport.ExportPdf();
        }

        private void iWord_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!Visible) return;
            MyExport.ExportRtf();
        }

        private void iExcel_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            MyExport.Export();
        }

        private void iPreview_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Preview();
        }

        private void Preview()
        {
        }

        private void iQuickPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Print();
        }

        private void Print()
        {
        }

        private void iPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            PrintDialog();
        }

        private void PrintDialog()
        {
        }

        private void iXem_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void iClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            //throw new NotImplementedException();
            if (!Visible) return;
            Close();
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            MyRibbon.IgClose.Visible = true;
            MyRibbon.iEdit.Visible = false;
            MyRibbon.iUndo.Visibility = BarItemVisibility.Always;
            MyRibbon.iCancel.Visibility = BarItemVisibility.Always;
            MyRibbon.isPrint.Visibility = BarItemVisibility.Always;
            MyRibbon.iSave.Visibility = BarItemVisibility.Always;
            MyRibbon.iClose.Visibility = BarItemVisibility.Always;
            MyRibbon.iRefesh.Visibility = BarItemVisibility.Always;
            // MyRibbon.iNavigate.Visible = false;
            MyRibbon.iComponents.Visibility = BarItemVisibility.Never;
            MyRibbon.iEquipHistory.Visibility = BarItemVisibility.Never;
            MyRibbon.iDelete.Visibility = BarItemVisibility.Never;
            MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
            MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
            MyRibbon.iClear.Visibility = BarItemVisibility.Never;

            MyRibbon.iXem.Visibility = BarItemVisibility.Never;
            MyRibbon.iExport.Visibility = BarItemVisibility.Never;
            MyRibbon.iWord.Visibility = BarItemVisibility.Never;
            MyRibbon.iPdf.Visibility = BarItemVisibility.Never;
            MyRibbon.iExcel.Visibility = BarItemVisibility.Never;
            MyRibbon.igPrint.Visible = true;
            MyRibbon.iPrint.Visibility = BarItemVisibility.Always;
            MyRibbon.iQuickPrint.Visibility = BarItemVisibility.Always;
            MyRibbon.iPreview.Visibility = BarItemVisibility.Always;


            MyRibbon.iClose.ItemClick += iClose_ItemClick;
            MyRibbon.iXem.ItemClick += iXem_ItemClick;
            MyRibbon.iPrint.ItemClick += iPrint_ItemClick;
            MyRibbon.iQuickPrint.ItemClick += iQuickPrint_ItemClick;
            MyRibbon.iPreview.ItemClick += iPreview_ItemClick;
            MyRibbon.iExcel.ItemClick += iExcel_ItemClick;
            MyRibbon.iWord.ItemClick += iWord_ItemClick;
            MyRibbon.iPdf.ItemClick += iPdf_ItemClick;
            MyRibbon.iRefesh.ItemClick += iRefesh_ItemClick;
            MyRibbon.iCancel.ItemClick += iCancel_ItemClick;
            MyRibbon.iSave.ItemClick += iSave_ItemClick;
            MyRibbon.IAdd.ItemClick += IAdd_ItemClick;

            uc.NapInf();
            uc.Nap();
        }

        void IAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Add();
        }

        private void iSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Save();
        }

        private void iCancel_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Cancel();
        }

        private void iRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            uc.Nap();
            uc.NapDTL();
        }

        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate(e);

            if (MyRibbon != null)
            {
                MyRibbon.IgClose.Visible = false;
                MyRibbon.iEdit.Visible = false;
                MyRibbon.iUndo.Visibility = BarItemVisibility.Never;
                MyRibbon.iCancel.Visibility = BarItemVisibility.Never;
                MyRibbon.isPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iSave.Visibility = BarItemVisibility.Never;
                MyRibbon.iClose.Visibility = BarItemVisibility.Never;
                MyRibbon.iRefesh.Visibility = BarItemVisibility.Never;
                // MyRibbon.iNavigate.Visible = false;
                MyRibbon.iComponents.Visibility = BarItemVisibility.Never;
                MyRibbon.iEquipHistory.Visibility = BarItemVisibility.Never;
                MyRibbon.iDelete.Visibility = BarItemVisibility.Never;
                MyRibbon.iCopy.Visibility = BarItemVisibility.Never;
                MyRibbon.iSelectAll.Visibility = BarItemVisibility.Never;
                MyRibbon.iClear.Visibility = BarItemVisibility.Never;

                MyRibbon.iXem.Visibility = BarItemVisibility.Never;
                MyRibbon.iExport.Visibility = BarItemVisibility.Never;
                MyRibbon.iWord.Visibility = BarItemVisibility.Never;
                MyRibbon.iPdf.Visibility = BarItemVisibility.Never;
                MyRibbon.iExcel.Visibility = BarItemVisibility.Never;
                MyRibbon.igPrint.Visible = false;
                MyRibbon.iPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iQuickPrint.Visibility = BarItemVisibility.Never;
                MyRibbon.iPreview.Visibility = BarItemVisibility.Never;


                MyRibbon.iClose.ItemClick -= iClose_ItemClick;
                MyRibbon.iXem.ItemClick -= iXem_ItemClick;
                MyRibbon.iPrint.ItemClick -= iPrint_ItemClick;
                MyRibbon.iQuickPrint.ItemClick -= iQuickPrint_ItemClick;
                MyRibbon.iPreview.ItemClick -= iPreview_ItemClick;
                MyRibbon.iExcel.ItemClick -= iExcel_ItemClick;
                MyRibbon.iWord.ItemClick -= iWord_ItemClick;
                MyRibbon.iPdf.ItemClick -= iPdf_ItemClick;
                MyRibbon.iRefesh.ItemClick -= iRefesh_ItemClick;
                MyRibbon.iCancel.ItemClick -= iCancel_ItemClick;
                MyRibbon.iSave.ItemClick -= iSave_ItemClick;
                MyRibbon.IAdd.ItemClick -= IAdd_ItemClick;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            Hide();
            //if (condition != null) condition.Close();
            e.Cancel = !Program._exit;
        }
    }
}